/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "network.h"
#include "route.h"

static Network_JsonPointerType jsp;
static char valuebuf[64];

static uint8_t route_cf_up_cycle(Route_PayloadType *rtpl)
{
  if((rtpl == NULL) || (rtpl->pl_len != sizeof(Cf_StateType)))
  {
    return 1;
  }
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  snprintf(valuebuf, 64, "%u", ((Cf_StateType*)(rtpl->payload))->cfkg_st);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cfkg", jsp.js_value);
  
  
  snprintf(valuebuf, 64, "%u", ((Cf_StateType*)(rtpl->payload))->cfst);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cfst", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID",  Route_str_pids[RT_DV_CF]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.cf[add2idx(rtpl->dev_addr)].did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
	
	return 0;
}

static uint8_t route_cf_up_sync(Route_PayloadType *rtpl)
{
  if( (rtpl == NULL) || (rtpl->pl_len != sizeof(Cf_SyncType)) )
  {
    return 1;
  }
  
  memset(&jsp, 0, sizeof(Network_JsonPointerType));
  memset(valuebuf, 0, sizeof(valuebuf));
  
  jsp.property = cJSON_CreateObject();
  
  snprintf(valuebuf, 64, "%u", ((Cf_SyncType*)(rtpl->payload))->state.cfkg_st);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cfkg", jsp.js_value);
  
  
  snprintf(valuebuf, 64, "%u", ((Cf_SyncType*)(rtpl->payload))->state.cfst);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cfst", jsp.js_value);
  
  
  snprintf(valuebuf, 64, "%u", ((Cf_SyncType*)(rtpl->payload))->para.cftmen);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cftmen", jsp.js_value);
  
  
  snprintf(valuebuf, 64, "%u-%u-%u", ((Cf_SyncType*)(rtpl->payload))->hour, \
                                     ((Cf_SyncType*)(rtpl->payload))->minute, \
                                     ((Cf_SyncType*)(rtpl->payload))->second);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cftime", jsp.js_value);
  
  
  snprintf(valuebuf, 64, "%u", ((Cf_SyncType*)(rtpl->payload))->para.cfot);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cfot", jsp.js_value);
  
  
  snprintf(valuebuf, 64, "%u-%u-%u-%u-%u-%u", ((Cf_SyncType*)(rtpl->payload))->para.cftm[0], \
                                              ((Cf_SyncType*)(rtpl->payload))->para.cftm[1], \
                                              ((Cf_SyncType*)(rtpl->payload))->para.cftm[2], \
                                              ((Cf_SyncType*)(rtpl->payload))->para.cftm[3], \
                                              ((Cf_SyncType*)(rtpl->payload))->para.cftm[4], \
                                              ((Cf_SyncType*)(rtpl->payload))->para.cftm[5]);
  jsp.js_value = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.js_value, "value", valuebuf);
  cJSON_AddItemToObject(jsp.property, "cftm", jsp.js_value);
  
  
  jsp.identity = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.identity, "productID", Route_str_pids[RT_DV_CF]);
  cJSON_AddStringToObject(jsp.identity, "deviceName", Route_DeviceList.cf[add2idx(rtpl->dev_addr)].did);
  
  jsp.arrayitem = cJSON_CreateObject();
  cJSON_AddItemToObject(jsp.arrayitem, "identity", jsp.identity);
  cJSON_AddItemToObject(jsp.arrayitem, "properties", jsp.property);
  
  jsp.params = cJSON_CreateArray();
  cJSON_AddItemToArray(jsp.params, jsp.arrayitem);
  
  jsp.root = cJSON_CreateObject();
  cJSON_AddStringToObject(jsp.root, "id", "1");
  cJSON_AddStringToObject(jsp.root, "version", ONEJSON_VERSION);
  cJSON_AddItemToObject(jsp.root, "params", jsp.params);
  
  cJSON_PrintPreallocated(jsp.root, Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, 0);

	snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Pack_Post, ROUTE_SELF_DID);
	
  cJSON_Delete(jsp.js_value);
  cJSON_Delete(jsp.root);
  cJSON_Delete(jsp.params);
  cJSON_Delete(jsp.arrayitem);
  cJSON_Delete(jsp.identity);
  cJSON_Delete(jsp.property);
  
  return 0;
}

uint8_t Route_cf_up(Route_PayloadType *rtpl)
{
  uint8_t err = 1;
  
  switch(rtpl->pl_type)
  {
		case ROUTE_PLTYP_CF_CYCLE:
			err = route_cf_up_cycle(rtpl);
			break;
    
    case ROUTE_PLTYP_CF_SYNC:
			err = route_cf_up_sync(rtpl);
			break;
    
    default:
      err = 1;
      break;
  }
  
  return err;
}
